問題描述
關閉模態對話框窗口後 ASP.NET 刷新父頁面 (ASP.NET Refresh Parent Page after Closing Modal Dialog Window)
I have an ASP.NET page that launches a modal dialog window:
Dim sURL As String = System.Configuration.ConfigurationManager.AppSettings("PAYORS_Path") & "PayorCopy.aspx"
lnkCopy.Attributes.Add("onclick", "javascript:window.showModalDialog('" & sURL & "',null,'status:no;dialogWidth:375px;dialogHeight:550px;dialogHide:true;help:no;scroll:yes;center:yes');return false;")
The user can create new items in this modal window. When the user clicks the Close button the modal dialog window is successfully closed, but the new items created do not appear on the parent page. When the modal window is closed I would like the parent page to refresh to show the new items the user created. Currently:
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnClose.Click
Dim strjscript As String = "<script language='javascript'>self.close();</script>"
LtClose.Text = strjscript
End Sub
I have tried adding to this script:
window.opener.location.reload(true);
But then when I test I get an error that says "Unable to get value of the property 'location': object is null or undefined."
Any help would be much appreciated!
‑‑‑‑‑
參考解法
方法 1:
That would be because there is no 'opener' :) Location is method of the window object so you can even treat it as global is most cases. Any of those should work:
window.location.reload(true);
or:
location.reload(true);
EDIT Yeah I just noticed where you put that script.. if it's in the click handle inside the modal..then 'window' there is really a different one ‑ it's modal after all! Try adding it directly after the code opening modal window ‑ the script execution will be blocked until the modal returns, so you can so something like this:
var shouldReloadPage = window.showModalDialog('" & sURL & "',null,'status:no;dialogWidth:375px;dialogHeight:550px;dialogHide:true;help:no;scroll:yes;center:yes');
if(shouldReloadPage){
window.location.reload(true);
}
return false;
This is still inside the click handler ofc, just showing in code block for easier reading.
方法 2:
Instead of using the old modal dialog window, try jQuery Dialog because it behaves just like a modal but keeps you in the same window. Therefore it's is easily refreshed with the following code:
location.reload(true);
For more information, check out this question here at SO.
Hope it helps.
(by MF Luder、Damyan Petev、Androiderson)